import torch
import numpy as np
import cv2 as cv


def letter_box(img, new_shape):
    """将图片等比例缩放调整到指定边长的正方形,剩下的填充"""
    shape = img.shape[:2]  # [h, w]
    r = min(new_shape / shape[0], new_shape / shape[1])  # scale ratio (new / old)
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    img = cv.resize(img, new_unpad, interpolation=cv.INTER_LINEAR)
    dw, dh = (new_shape - new_unpad[0]) / 2, (new_shape - new_unpad[1]) / 2  # wh padding
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))  # 计算上下两侧的padding
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))  # 计算左右两侧的padding
    img = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_CONSTANT, value=(0, 0, 0))  # add border
    return img
